const TokenKey = 'X-Token-H5';

export function getToken() {
  let token = '';
  const requestParams = GetRequest();
  if (requestParams && requestParams.token) {
    token = requestParams.token.split('#')[0];
    setToken(token);
  } else {
    token = localStorage.getItem(TokenKey);
  }
  return token;
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token);
}

export function GetRequest() {
  let url = location.search; // 获取url中'?'符后的字串
  if (url.indexOf('token') == -1) {
    url = location.hash;
  }
  if (url.indexOf('token') == -1) {
    url = location.href;
  }
  let requestParams = new Object();
  if (url.indexOf('?') != -1) {
    let str = url.substr(url.indexOf('?') + 1);
    let strs = str.split('&');
    for (let i = 0; i < strs.length; i++) {
      requestParams[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
    }
  }
  return requestParams;
}
